<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="供求类型" prop="infoType">
        <el-select
            v-model="queryParams.infoType"
            placeholder="请选择供求类型"
            clearable
            style="width: 180px"
            @change="handleQuery"
        >
          <el-option
              v-for="item in infoTypeOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
          />

        </el-select>
      </el-form-item>

      <!-- 新增的信息内容搜索框 -->
      <el-form-item label="信息内容" prop="content">
        <el-input
            v-model="queryParams.content"
            placeholder="请输入信息内容"
            clearable
            @keyup.enter="handleQuery"
        />
      </el-form-item>

      <el-form-item label="企业全称" prop="companyName">
        <el-input
          v-model="queryParams.companyName"
          placeholder="请输入企业全称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="userNickname">
        <el-input
          v-model="queryParams.userNickname"
          placeholder="请输入用户昵称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="mobileNumber">
        <el-input
          v-model="queryParams.mobileNumber"
          placeholder="请输入手机号码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:supply:remove']"
        >删除</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
        v-loading="loading"
        :data="supplyList"
        @selection-change="handleSelectionChange"
        fit
        style="width: 100%"
        :show-header-overflow="true"
        :show-overflow-tooltip="true"
    >
      <el-table-column type="selection" width="55" align="center"  fixed=left :resizable="false"/>
<!--      <el-table-column label="信息ID" align="center" prop="id" />-->
<!--      <el-table-column label="操作" align="center" prop="operation" />-->

      <el-table-column label="操作" align="center" fixed=left class-name="small-padding fixed-width" :resizable="false" min-width="120">
        <template #default="scope">
          <el-button link type="primary"  @click="handleView(scope.row)" v-hasPermi="['system:supply:view']">查看</el-button>
          <text> | </text>
          <el-button link type="primary"  @click="handleDelete(scope.row)" v-hasPermi="['system:supply:remove']">删除</el-button>
        </template>
      </el-table-column>
      <el-table-column label="供求类型" align="center" prop="infoType" min-width="120"/>
      <el-table-column label="信息内容" align="center" prop="content" min-width="120"/>
      <el-table-column label="联系电话" align="center" prop="contactPhone" min-width="150"/>
      <el-table-column label="行业标签" align="center" prop="industryTag" min-width="120"/>
      <el-table-column label="企业全称" align="center" prop="companyName" min-width="200"/>
      <el-table-column label="用户昵称" align="center" prop="userNickname" min-width="120"/>
      <el-table-column label="手机号码" align="center" prop="mobileNumber" min-width="120"/>
      <el-table-column label="发布时间" align="center" prop="publishTime" min-width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.publishTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="浏览量" align="center" prop="viewCount" />

    </el-table>

    <!-- 查看详情弹出框 -->
    <el-dialog
        :title="'详情'"
        v-model="detailVisible"
        width="800px"
        class="right-dialog"
        append-to-body
    >
      <div class="detail-card" style="font-size: small ;line-height: 40px">
        <div class="detail-header">
          <span class="label">供求类型：</span>
          <span class="value">{{ detailForm.infoType }}</span>
        </div>
        <div class="detail-content">
          <div class="content-text">
            <span class="label">信息内容：</span>
            <span class="value">{{ detailForm.content }}</span>
          </div>

          <div class="content-contact">
            <el-row :gutter="16">
              <el-col :span="12">
                <span class="label">联系电话: </span>
                <span class="value">{{ detailForm.contactPhone }}</span>
              </el-col>
              <el-col :span="13">
                <span class="label">行业标签: </span>
                <span class="value">{{ detailForm.industryTag }}</span>
              </el-col>
              <el-col :span="14">
                <span class="label">企业全称: </span>
                <span class="value">{{ detailForm.companyName }}</span>
              </el-col>
              <el-col :span="15">
                <span class="label">用户昵称: </span>
                <span class="value">{{ detailForm.userNickname }}</span>
              </el-col>
              <el-col :span="16">
                <span class="label">手机号码: </span>
                <span class="value">{{ detailForm.mobileNumber }}</span>
              </el-col>
              <el-col :span="17">
                <span class="label">发布时间: </span>
                <span class="value">{{ parseTime(detailForm.publishTime) }}</span>
              </el-col>
              <el-col :span="18">
                <span class="label">浏览量: </span>
                <span class="value">{{ detailForm.viewCount }}</span>
              </el-col>
            </el-row>
          </div>
        </div>
      </div>

      <template #footer>
        <div class="dialog-footer">
          <el-button @click="detailVisible = false" style="align-content: center">关闭</el-button>
        </div>
      </template>
    </el-dialog>
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改供求信息管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="supplyRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="供求类型" prop="infoType">
          <el-select v-model="form.infoType" placeholder="请选择供求类型" style="width: 100%">
            <el-option label="供应信息" value="supply" />
            <el-option label="需求信息" value="demand" />
          </el-select>
        </el-form-item>
        <el-form-item label="信息内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="联系电话" prop="contactPhone">
          <el-input v-model="form.contactPhone" placeholder="请输入联系电话" />
        </el-form-item>
        <el-form-item label="行业标签" prop="industryTag">
          <el-input v-model="form.industryTag" placeholder="请输入行业标签" />
        </el-form-item>
        <el-form-item label="企业全称" prop="companyName">
          <el-input v-model="form.companyName" placeholder="请输入企业全称" />
        </el-form-item>
        <el-form-item label="用户昵称" prop="userNickname">
          <el-input v-model="form.userNickname" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="手机号码" prop="mobileNumber">
          <el-input v-model="form.mobileNumber" placeholder="请输入手机号码" />
        </el-form-item>
        <el-form-item label="发布时间" prop="publishTime">
          <el-date-picker clearable
            v-model="form.publishTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择发布时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="浏览量" prop="viewCount">
          <el-input v-model="form.viewCount" placeholder="请输入浏览量" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Supply">
import { listSupply, getSupply, delSupply, addSupply, updateSupply } from "@/api/system/supply.js"

const { proxy } = getCurrentInstance()

const supplyList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const infoTypeOptions = ref([])

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    infoType: null,
    content: null,
    companyName: null,
    userNickname: null,
    mobileNumber: null,
  },
  rules: {
    infoType: [
      { required: true, message: "供求类型不能为空", trigger: "change" }
    ],
    content: [
      { required: true, message: "信息内容不能为空", trigger: "blur" }
    ],
    contactPhone: [
      { required: true, message: "联系电话不能为空", trigger: "blur" }
    ],
    industryTag: [
      { required: true, message: "行业标签不能为空", trigger: "blur" }
    ],
    userNickname: [
      { required: true, message: "用户昵称不能为空", trigger: "blur" }
    ],
    mobileNumber: [
      { required: true, message: "手机号码不能为空", trigger: "blur" }
    ],
    publishTime: [
      { required: true, message: "发布时间不能为空", trigger: "blur" }
    ],
    createTime: [
      { required: true, message: "创建时间不能为空", trigger: "blur" }
    ],
    updateTime: [
      { required: true, message: "更新时间不能为空", trigger: "blur" }
    ]
  }
})

//弹出框响应数据
const detailVisible = ref(false)
const detailForm = ref({
  id: null,
  auditStatus: null,
  infoType: null,
  content: null,
  contactPhone: null,
  industryTag: null,
  companyName: null,
  userNickname: null,
  mobileNumber: null,
  publishTime: null,
  viewCount: null,
  companyLogo: null
})
const { queryParams, form, rules } = toRefs(data)

/** 查询供求信息管理列表 */
function getList() {
  loading.value = true
  listSupply(queryParams.value).then(response => {
    supplyList.value = response.rows
    total.value = response.total
    const allInfoTypes = response.rows.map(item => item.infoType)
    const uniqueInfoTypes = [...new Set(allInfoTypes)].filter(type => type)
    infoTypeOptions.value = uniqueInfoTypes.map(type => ({
      value: type,
      label: type
    }))

    loading.value = false
  }).catch(() => {
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    operation: null,
    infoType: null,
    content: null,
    contactPhone: null,
    industryTag: null,
    companyName: null,
    userNickname: null,
    mobileNumber: null,
    publishTime: null,
    viewCount: null,
    createTime: null,
    updateTime: null
  }
  proxy.resetForm("supplyRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加供求信息管理"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _id = row.id || ids.value
  getSupply(_id).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改供求信息管理"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["supplyRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateSupply(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addSupply(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value
  proxy.$modal.confirm('是否确认删除供求信息管理编号为"' + _ids + '"的数据项？').then(function() {
    return delSupply(_ids)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/supply/export', {
    ...queryParams.value
  }, `supply_${new Date().getTime()}.xlsx`)
}

getList()
//查看
function handleView(row) {
  detailForm.value = JSON.parse(JSON.stringify(row))
  detailVisible.value = true
}
</script>
<style>
*{
  margin: 0;
  padding: 0;
}
.right-dialog {
  position: fixed;
  right: 0;
  top: 0;
  width: 800px;
  max-width: 50vw;
  border-radius: 0;
}

.right-dialog .el-dialog {
  margin: 0 !important;
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.right-dialog .el-dialog__header {
  position: sticky;
  top: 0;
  background: #fff;
  z-index: 10;
  border-bottom: 1px solid #eee;
  padding: 15px 20px !important;
  margin: 0 !important;
}

.right-dialog .el-dialog__body {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
  margin: 0;
}

.right-dialog .el-dialog__footer {
  position: sticky;
  bottom: 0;
  background: #fff;
  z-index: 10;
  border-top: 1px solid #eee;
  padding: 15px 20px;
  text-align: center;
  margin: 0;
}

/* 详情卡片样式优化 */
.detail-card {
  font-size: small;
  line-height: 1.6;
  padding: 0;
}

.detail-card .label {
  font-weight: bold;
  color: #606266;
  min-width: 100px;
  display: inline-block;
  vertical-align: top;
}

.detail-card .value {
  display: inline-block;
  max-width: calc(100% - 100px);
}

/* 图片列表样式优化 */
.image-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin: 10px 0;
}

.detail-image {
  max-width: 150px;
  max-height: 150px;
  border-radius: 4px;
  object-fit: cover;
  border: 1px solid #eee;
}

/* 响应式调整 */
@media (max-width: 992px) {
  .right-dialog {
    width: 100%;
    max-width: 100%;
  }
}
</style>